package com.course.controller.admin;

import com.course.confing.JwtUtils;
import com.course.entity.Result;
import com.course.entity.Teacher;
import com.course.server.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/admin/login")
public class AdminLoginController {
    @Autowired
    private TeacherService teacherService;
    @PostMapping
    public Result login(@RequestBody Teacher teacher){
        Teacher e=teacherService.login(teacher);
        //登录成功，生成令牌，下发令牌
        if(e!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",e.getTno());
            claims.put("name",e.getTname());

            String jwt= JwtUtils.generateJwt(claims);
            return Result.sucess(jwt);
        }
        return Result.error("账号或者密码错误");
    }
}
